#include "stdafx.h"
#include ".\config.h"
#include "io.h"

PTCHAR DFT_CONFIG_FN = _T("C:\\Program Files\\Seven\\MyTools\\config.ini"); //default config file name
PTCHAR KN_CONFIG_FN = _T("ConfigFile");                                     //key name to recevie config file name in REG

config::config(void)
{
	getConfigFilePath();
	if( _taccess(m_szConfigFile,06) == 0 ){
		//file exists
		readConfigFile(m_szConfigFile);
	}else{
		//file does not exists
		getDefaultConfig();
		writeConfigFile(m_szConfigFile);
	}
}

config::~config(void)
{
}

void config::getConfigFilePath()
{
	CRegKey reg;
	LONG    lRet;
    bool	succeed;
	DWORD	iSize = MAX_PATH;

	succeed = false;
    //try to read from REG first
	lRet = reg.Open ( HKEY_CURRENT_USER, _T("SOFTWARE\\Seven\\MyTools\\shellext"),
					KEY_READ |KEY_WRITE);

	if ( ERROR_SUCCESS == lRet ){
		lRet = reg.QueryValue(	KN_CONFIG_FN,
								NULL,
								m_szConfigFile,
								&iSize );

		if ( ERROR_SUCCESS == lRet ){
			succeed = true;
		}else{
			reg.SetValue( KN_CONFIG_FN,
                          REG_SZ,
                          DFT_CONFIG_FN,
                          _tcslen(DFT_CONFIG_FN));
		}
		reg.Close();
	}
	if(!succeed){
		_tcscpy(m_szConfigFile,DFT_CONFIG_FN);
	}

	
}
void config::getDefaultConfig()
{
	DonnetInstalled = false;
	showCMDMenu = true;
	showRegDllMenu = true;
	showCopyFilePath = true;

	_tcscpy(m_szDonnetParameter,_T("/k \"C:\\Program Files\\Microsoft Visual Studio .NET 2003\\Common7\\Tools\\vsvars32.bat\""));
	m_szCMDParameter[0] = 0;
}

bool config::readConfigFile(LPCTSTR cfgfn)
{
	//isDonnetInstalled
	DonnetInstalled = GetPrivateProfileInt(_T("DirBkgndExt"),
		_T("DonnetInstalled"),
		0,
		cfgfn) != 0;

	//Donnet init batch file path
	if(DonnetInstalled){
		GetPrivateProfileString(_T("DirBkgndExt"),
			_T("DonnetInitBatchFile"),
			_T("/k \"C:\\Program Files\\Microsoft Visual Studio .NET 2003\\Common7\\Tools\\vsvars32.bat\""),
			m_szDonnetParameter,
			MAX_PATH,
			cfgfn);
	}


	showCMDMenu = GetPrivateProfileInt(_T("DirBkgndExt"),
		_T("showCMDMenu"),
		0,
		cfgfn) != 0;
	
    if (showCMDMenu) {
        //CMD init file path
		GetPrivateProfileString(_T("DirBkgndExt"),
			_T("CMDRCFILE"),
			_T(""),
			m_szCMDParameter,
			MAX_PATH,
			cfgfn);
    }
	showRegDllMenu = GetPrivateProfileInt(_T("DirBkgndExt"),
		_T("showRegDllMenu"),
		0,
		cfgfn) != 0;

	showCopyFilePath = GetPrivateProfileInt(_T("DirBkgndExt"),
		_T("showCopyFilePath"),
		0,
		cfgfn) != 0;

	return true;

}

void config::writeConfigFile(LPCTSTR cfgfn)
{
	TCHAR buf[MAX_PATH];

	_itot(DonnetInstalled?1:0,buf,10);
    WritePrivateProfileString(_T("DirBkgndExt"),
		_T("DonnetInstalled"),
		buf,
		cfgfn);

	_itot(showCMDMenu?1:0,buf,10);
    WritePrivateProfileString(_T("DirBkgndExt"),
		_T("showCMDMenu"),
		buf,
		cfgfn);

	_itot(showRegDllMenu?1:0,buf,10);
    WritePrivateProfileString(_T("DirBkgndExt"),
		_T("showRegDllMenu"),
		buf,
		cfgfn);

	_itot(showCopyFilePath?1:0,buf,10);
    WritePrivateProfileString(_T("DirBkgndExt"),
		_T("showCopyFilePath"),
		buf,
		cfgfn);
	
    WritePrivateProfileString(_T("DirBkgndExt"),
		_T("DonnetInitBatchFile"),
		m_szDonnetParameter,
		cfgfn);

    WritePrivateProfileString(_T("DirBkgndExt"),
		_T("CMDRCFILE"),
		m_szCMDParameter,
		cfgfn);
}